const fs = require("fs");
const path = require("path");

function render(res, url, status = 200) { 
    //js python 默认值写法就是在形参上面直接赋值
    res.writeHead(status, {
        "content-type": "text/html;charset=utf-8",
    });
    fs.readFile(path.join(__dirname, url), "utf-8", (err, data) => {
        if (err) {
            console.log("页面读取失败");
        } else {
            res.end(data);
        }
    });
}

function router(url, res) {
    if (url === "/home") {
        render(res, './static/home.html')
    } else if (url === "/login") {
        render(res, './static/login.html')
    } else {
        render(res, './static/404.html', 404)
    }
}

module.exports = router;